import { customerExcelApi, disabledCustomerApi, enabledCustomerApi, getListApi, restCustomerPasswordApi } from "@/api/managementCenter/customerManagement";
import { Modal, message } from "ant-design-vue";
import { computed, createVNode, h } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import dayjs from "dayjs";
export default function OperationHook(state: Base.GeneraDataType) {
  const { drawerOpts, searchForm, tableRef, tableData, loading, dialogOpts } = state;
  const selectRows = computed<Array<DepartmentPersonnel.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });

  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  const getData = () => {
    const params = {
      ...searchForm
    };
    const { createTime } = searchForm;
    if (params.createTime && createTime.length) {
      params.createTime = [dayjs(createTime[0]).format("YYYY-MM-DD HH:mm:ss"), dayjs(createTime[1]).format("YYYY-MM-DD HH:mm:ss")];
    }
    loading.value = true;
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => (loading.value = false));
  };
  const drawerSubmitHandle = () => {
    drawerOpts.visible = false;
    getData();
  };
  const addHandle = () => {
    drawerOpts.title = "新增客户";
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "AddCustomer";
    drawerOpts.visible = true;
  };
  const editHandle = (row: CustomerManagement.ListRows) => {
    drawerOpts.title = "编辑客户";
    drawerOpts.width = "1200px";
    drawerOpts.type = 2;
    drawerOpts.passData = row;
    drawerOpts.component = "AddCustomer";
    drawerOpts.visible = true;
  };
  // 修改状态
  const changeStatusHandle = (row: CustomerManagement.ListRows) => {
    const params: any = {
      id: row.id
    };
    let tip = null;
    let requestFn: any = null;
    if (row.status === 1) {
      tip = "是否停用该客户?";
      params.status = 0;
      requestFn = disabledCustomerApi;
    } else {
      tip = "是否启用该客户?";
      params.status = 1;
      requestFn = enabledCustomerApi;
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        requestFn(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            getData();
          }
        });
      }
    });
  };
  // 批量修改
  const batchModifyHandle = (type: number) => {
    if (!selectRows.value.length) return message.error("请先勾选数据!");
    let tip;
    if (type === 1) {
      tip = "批量修改业务员";
    } else {
      tip = "批量修改跟单";
    }
    dialogOpts.title = tip;
    dialogOpts.width = "520px";
    dialogOpts.component = "BatchModify";
    dialogOpts.type = type;
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  //批量导出
  const exportHandle = () => {
    const params = {
      ...searchForm,
      list: selectRows.value.length ? selectRows.value : undefined
    };
    return customerExcelApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("创建任务成功，请前往导出中心下载！");
        getData();
      }
    });
  };
  //重置密码
  const restCustomerPasswordHandle = (row: CustomerManagement.ListRows) => {
    Modal.confirm({
      title: h("div", { style: { display: "flex", justifyContent: "center", flexDirection: "column" } }, [h("p", "是否重置密码?确认后将退出登录"), h("p", "重置密码:123456")]),
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      async onOk() {
        const restPassword: any = await restCustomerPasswordApi({ id: row.id, password: "123456" });
        const { code } = restPassword;

        if (code === 200) {
          message.success("操作成功");
          getData();
          //退出登录
          // const logOut: any = await loginOutApi();
          // if (logOut.code === 200) {
          //   clearAuthent();
          //   PermissionStore.asyncRoutes = [];
          //   PermissionStore.clearCacheList();
          //   resetRouter();
          //   router.replace("/login");
          // }
        }
      }
    });
  };

  return {
    searchHandle,
    selectRows,
    getData,
    drawerSubmitHandle,
    addHandle,
    editHandle,
    changeStatusHandle,
    batchModifyHandle,
    exportHandle,
    restCustomerPasswordHandle
  };
}
